const request = require('superagent')
const cheerio = require('cheerio')
const fs = require('fs')

const reptileUrl = 'http://www.jianshu.com/'

function fileterText(text) {
  return text.replace(/\n/g, '').replace(/\s/g, '')
}
request
  .get(reptileUrl)
  .set(
    'User-Agent',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3464.0 Safari/537.36'
  )
  .end((err, res) => {
    if (err) {
      throw Error(err)
    }
    let $ = cheerio.load(res.text)
    let data = []
    $('#list-container .note-list li').each((i, elem) => {
      let _this = $(elem)
      data.push({
        id: _this.attr('data-note-id'),
        thumbnails: _this.find('.wrap-img img').attr('src'),
        slug: _this
          .find('.title')
          .attr('href')
          .replace(/\/p\//, ''),
        title: fileterText(_this.find('.title').text()),
        abstract: fileterText(_this.find('.abstract').text()),
        meta: {
          nickname: fileterText(_this.find('.nickname').text()),
          link: _this
            .find('.nickname')
            .attr('href')
            .replace(/\/u\//, ''),
          comments_count: +fileterText(
            _this
              .find('.ic-list-comments')
              .parent()
              .text()
          ),
          likes_count: +fileterText(
            _this
              .find('.ic-list-like')
              .parent()
              .text()
          )
        }
      })
    })
    // fs.writeFile(
    //   __dirname + '/data/article.json',
    //   JSON.stringify({ status: 0, data: data }),
    //   err => {
    //     if (err) throw err
    //     console.log('文件保存成功')
    //   }
    // )
    let writeStream = fs.createWriteStream(__dirname + '/data/article.json')
    writeStream.write(JSON.stringify({ status: 0, data: data }), 'utf-8')
    writeStream.end()
    writeStream.on('finish', () => {
      console.log('写入完成')
    })
    writeStream.on('error', () => {
      console.log('写入失败')
    })
  })
